﻿/*
 * Created by SharpDevelop.
 * User: rogerio
 * Date: 01/03/2010
 * Time: 14:24
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using csharp.common;
using System.Data;

namespace csharp.common
{
	/// <summary>
	/// Description of Search.
	/// </summary>
	
public class Search
{
    private List<String> _tables = new List<String>();
    private List<String> _fields = new List<String>();
    private List<String> _statements  = new List<String>();
    private List<String> _conditionalWhere = new List<String>();
    private List<String> _junction = new List<String>();
    private List<String> _additional = new List<String>();
    //private GenerateSql _gsql = new GenerateSql();
    private DataSet _rs;
    
    public Search()
    {

    }

    public void prepare()
    {
    	_tables.Clear();
    	_fields.Clear();
        _statements.Clear();
        _conditionalWhere.Clear();
        _junction.Clear();
        _additional.Clear();
    }

    public void setTable(String value)
    {
        _tables.Add(value);
        _statements.Add("");
    }

    public void setTable(String table, String onStatement)
    {
        _tables.Add(table);
        _statements.Add(onStatement);
    }

    public void setOnStatement(String onStatement)
    {
        _statements.Add(onStatement);
    }

    public void setConditionalWhere(String value)
    {
        _conditionalWhere.Add(value);
    }

    public void setAdditional(String value)
    {
        _additional.Add(value);
    }

    public void setFields(String field)
    {
        _fields.Add(field);
    }

    public void setFields(String []array)
    {
        for(int i=0; i < array.Length; i++)
            _fields.Add(array[i]);
    }

    public String getSql()
    {
        return mountSql();
    }

    public DataSet getResultSet()
    {
        return _rs;
    }

    public String getMessageError()
    {
        return Database.result.ErrorMessage;
    }

    public Boolean execute()
    {
        String sql = mountSql();

        _rs = Database.ExecuteQuery(sql);
        return (Database.result.Status == Database.Status.SUCCESSFUL);
    }

    private String mountSql()
    {
        String sSQL;
        String joins;
        String sWhere;
        String aux;
        String additional = "";

        /* get name of fields*/
        sSQL = "";
        for (int i = 0; i < _fields.Count; i++)
        {
        	if (!_fields[i].Equals(""))
            {
                if (sSQL.Equals(""))
                	sSQL = _fields[i];
                else
                	sSQL = sSQL + ", " + _fields[i];
            }
        }

        /*get joins*/
        joins = _tables[0];
        for(int i=1; i < _tables.Count; i++)
        {
        	joins+= " JOIN "+_tables[i]+" ON "+_statements[i];
        }

         /* get where*/
        if (_conditionalWhere.Count!= 0)
        {
        	sWhere = " WHERE " + _conditionalWhere[0];
                for (int i = 1; i < _conditionalWhere.Count; i++)
                {
                        if (_junction.Count >= i)
                        	aux = _junction[i-1];
                        else
                                aux = "AND";
                        sWhere += " "+aux+" "+_conditionalWhere[i];
                }
        }
        else
                sWhere = "";

        //additional
        for (int i=0; i < _additional.Count; i++)
        {
        	additional+= " "+_additional[i];
        }
        return "SELECT " +sSQL + " FROM "+ joins + sWhere + " " +additional;
    }
}
}